package org.example;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class Test {

    private static void method1() throws InterruptedException {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //定位输入框并输入内容"软件测试"
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        //点击"百度一下"按钮进行百度
        webDriver.findElement(By.cssSelector("#su")).click();

        //找到所有的"软件测试"文案对应元素
        sleep(4000);//强制等待4s,目的是等待html页面进行渲染,否则先运行定位元素操作容易出现查找不到元素的问题

        List<WebElement> em = webDriver.findElements(By.cssSelector("em"));//找到em标签并存储在list集合中
        //输出找到元素的个数
        System.out.printf(String.valueOf(em.size()));
        System.out.println();
        if (String.valueOf(em.size()).equals("0")) {
            System.out.println("测试不通过");
        } else {
            System.out.println("测试通过");
        }

        //关闭跳转页面
        webDriver.quit();
    }

    private static void method2() {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com/");

        System.out.println(webDriver.findElement(By.cssSelector("#su")).getAttribute("value"));
        System.out.println(webDriver.findElement(By.cssSelector("#su")).getText());

        //关闭跳转页面
        webDriver.quit();

    }

    private static void method3() {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //定位输入框并输入内容"软件测试"
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        //点击"百度一下"按钮进行百度
        webDriver.findElement(By.cssSelector("#su")).click();

        //找到所有的"软件测试"文案对应元素
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        List<WebElement> em = webDriver.findElements(By.cssSelector("em"));//找到em标签并存储在list集合中
        //输出找到元素的个数
        System.out.printf(String.valueOf(em.size()));
        System.out.println();
        if (String.valueOf(em.size()).equals("0")) {
            System.out.println("测试不通过");
        } else {
            System.out.println("测试通过");
        }
        webDriver.quit();
    }

    private static void method4() {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com/");

        //获取当前页面url
        String url = webDriver.getCurrentUrl();
        //打印当前页面url在控制台,方便调试
        System.out.println(url);
        //判断url是否等于https://www.baidu.com/
        if (url.equals("https://www.baidu.com/")) {
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
        webDriver.quit();
    }

    private static void method5() {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //页面最大化
        webDriver.manage().window().maximize();

        //获取当前页面title
        String title = webDriver.getTitle();
        System.out.println(title);
        //判断title是否等于百度一下，你就知道
        if (title.equals("百度一下，你就知道")) {
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
        webDriver.quit();

    }

    private static void method6() {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //定位输入框并输入内容"软件测试"
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        //点击"百度一下"按钮进行百度
        webDriver.findElement(By.cssSelector("#su")).click();
        //智能等待3min
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        //创建一个鼠标对象
        Actions actions = new Actions(webDriver);
        WebElement element = webDriver.findElement(By.cssSelector("#s_tab > div > a.s-tab-item.s-tab-item_1CwH-.s-tab-tieba_3gnzZ.s-tab-tieba"));
        actions.contextClick(element).perform();


    }

    private static void method7() {
        //创建驱动
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);

        //打开写好的html页面
        webDriver.get("file:///E:/Wangjiawei_git/frontend-code-exercise/%E8%87%AA%E5%8A%A8%E5%8C%96%E9%80%89%E6%8B%A9%E5%A4%9A%E7%BB%84%E5%85%83%E7%B4%A0.html");
        //获取所有的input标签元素
        List<WebElement> input = webDriver.findElements(By.cssSelector("input"));
        //遍历该集合,并执行需要执行的操作
        for (int i = 0; i < input.size(); i++) {
            if (input.get(i).getAttribute("type").equals("checkbox")) {
                input.get(i).click();
            }

        }
    }

    private static void method8() {
        //创建浏览器驱动
        WebDriver webDriver = new ChromeDriver();
        //打开html页面
        webDriver.get("file:///E:/Wangjiawei_git/frontend-code-exercise/select.html");

        //操作下拉框
        Select select = new Select(webDriver.findElement(By.cssSelector("#ShippingMethod")));
        select.selectByValue("12.51");
        select.selectByIndex(2);

    }

    private static void method9() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        WebDriverWait wait = new WebDriverWait(webDriver, 50);
        wait.until(ExpectedConditions.titleIs("百度一下，你就知道"));
    }

    private static void method10() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        ((JavascriptExecutor) webDriver).executeScript("document.documentElement.scrollTop=10000");
    }

    //截图操作,但未设置保存路径
    private static void method11() throws IOException {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        File file = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File(""));
    }

    private static void method12() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        webDriver.close();
    }

    private static void method13() {
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        //获取浏览器所有窗口句柄
        Set<String> windowHandles = webDriver.getWindowHandles();
        String target_handle = "";
        for (String handle : windowHandles) {
            target_handle = handle;
        }
        //输出拿到的页面窗口的最后一个句柄(标签页)
        System.out.println(target_handle);
        //切换操作对象
        webDriver.switchTo().window(target_handle);
    }

    public static void main(String[] args) throws InterruptedException {
        //method1();
        //method2();
        //method3();
        //method4();
        //method5();
        //method6();
        //method7();
        //method8();
        method10();
    }
}
